<template>
  <div style="display: inline-block;margin-left: 10px;position: relative; top: 12px;">
<el-upload class="upload-demo" :action="drurl" :headers="head" :show-file-list="false"
				 :on-success="handleAvatarSuccess" :on-error="handleAvatarError">
					<el-button size="small" type="primary">表单导入</el-button>
				</el-upload>
      	<!-- 导入弹层 -->
      <el-dialog title="导入" :visible.sync="dialogFormVisible1" ref="">
      	<el-table size="medium" border style="width: 100%" ref="tableData" :data="cuowuDate">
      		<el-table-column prop="errRow" label="错误行">
      		</el-table-column>
      		<el-table-column prop="errCell" label="错误列">
      		</el-table-column>
      		<el-table-column prop="message" label="错误内容">
      		</el-table-column>
      	</el-table>
      	<p style="color: #E02E2E;text-align: center;padding: 10px 0;">以上数据无法导入到系统，请确认是否继续导入</p>
      	<div slot="footer" class="dialog-footer">
      		<el-button @click="dialogFormVisible1 = false">否</el-button>
      		<el-button type="primary" @click="confirmLeading">是</el-button>
      	</div>
      </el-dialog>
      </div>
</template>

<script>
	export default {
		name: 'upload',
		props:['drurl','qrurl'],
		  computed:{
			  head(){
			  	return{
					Authorization:localStorage.getItem('authorization')
				}
			  },
		  },
		  data(){
			  return{
				   dialogFormVisible1: false,
				  cuowuDate: [], //导入错误列表数据
				  delCode: '', //导入时出现错误的一个标识，用于确认导入接口的传参
			  }
		  },
		methods: {
		//导入
					handleAvatarSuccess(res, file) {console.log(res)
						if(res.data == ""){
							if (res.statusCode == 20002) {
								this.$message.error(res.msg);
							}
							return false;
						}
						let data = JSON.parse(res.data);
						if(data&&data.code){
							if (data.code == "errorCode") {
								this.dialogFormVisible1 = true;
								this.cuowuDate = data.errors;
								this.delCode = data.delCode;
								//this.$message.error(data.message);
							} else if (data.errorCode == '-1') {
								this.$message.error(data.message);
							} else {
								this.$message({
									message: '上传成功',
									type: 'success'
								});
								this.$emit('test',true)
							}
						}else{
								if(res.statusCode == '200'){
									this.$message({
										message: '上传成功',
										type: 'success'
									});
									this.$emit('test',true)
								}
							}
						
					},
					handleAvatarError(err, file, fileList) {

					},
					//确认导入
					confirmLeading() {
						this.$get(this.qrurl, {
								delCode: this.delCode
							})
							.then(res => {
								if (res.data.statusCode == 200) {
									this.dialogFormVisible1 = false;
									this.$emit('test',true)
									this.$message({
										message: res.data.data,
										type: 'success'
									});
								} else if(res.data.statusCode == 20002){
									this.$message.error(res.data.msg);
								} else {
									this.dialogFormVisible1 = false;
									this.$message.error(res.data.msg);
								}
							});
					},

		}
	};
</script>
